package org.jthompson.monsoon;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.HashSet;
import java.util.List;
import java.util.Map;
import java.util.Set;

import org.apache.log4j.Logger;

public class EventMapping
{
	private static EventMapping instance = null;
	
	private EventMapping() 
	{ 
		 
	}
	
	public static synchronized EventMapping instance()
	{
		
		if( instance == null)
		{
			instance = new EventMapping();
			log.info("Creating new EventMapping instance");
		} else {
			log.info("Returning existing EventMapping instance");
		}
		return instance;
	}
	
	private Map<String, Set<Object>> eventNameObjectMap = new HashMap<String, Set<Object>>();
	
	public void addEventMapping(String eventName, Object destination)
	{
		synchronized(eventNameObjectMap)
		{
			if(eventNameObjectMap.get(eventName) == null)
			{
				Set<Object> objList = new HashSet<Object>();
				objList.add(destination);
				eventNameObjectMap.put(eventName, objList);
			} else {
				Set<Object> objList = eventNameObjectMap.get(eventName);
				objList.add(destination);
			}
		}
	}
	
	public List<Object> getEventMapping(String eventName)
	{
		synchronized(eventNameObjectMap)
		{
			return new ArrayList<Object>(eventNameObjectMap.get(eventName));
		}
	}
	
	private final static Logger log = Logger.getLogger(EventMapping.class);
}
